<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">客户名称</label>
                    <div class="layui-input-block">
                        <input type="text" name="name" placeholder="请输入" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <button class="layui-btn layuiadmin-btn-admin layui-btn-sm" lay-submit lay-filter="LAY-user-back-search">
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                    </button>
                </div>
            </div>
        </div>
        <div class="layui-card-body">
            <div style="padding-bottom: 10px;">
                <button class="layui-btn layuiadmin-btn-admin layui-btn-sm" title="刷新" data-type="refresh"><i
                        class="layui-icon layui-icon-refresh-3"></i>
                </button>
                <button class="layui-btn layuiadmin-btn-admin layui-btn-sm {$auth->check('customer/add')?$auth->check('customer/add'):'hide'}"
                        title="添加" data-type="add"><i
                        class="layui-icon layui-icon-add-1"></i>添加
                </button>
                <button class="layui-btn layuiadmin-btn-admin layui-btn-sm {$auth->check('customer/followup')?$auth->check('customer/followup'):'hide'}"
                        title="写跟进" data-type="follow_up"><i
                        class="iconfont icon-genjin_line"></i>写跟进
                </button>
                <button class="layui-btn layuiadmin-btn-admin layui-btn-sm {$auth->check('customer/deliver')?$auth->check('customer/deliver'):'hide'}"
                        title="转交" data-type="deliver"><i
                        class="layui-icon layui-icon-share"></i>转交
                </button>
                <button class="layui-btn layuiadmin-btn-admin layui-btn-sm {$auth->check('customer/export')?$auth->check('customer/export'):'hide'}"
                        title="导出" data-type="export"><i
                        class="iconfont icon-daochu3"></i>导出
                </button>
                <button class="layui-btn layuiadmin-btn-admin layui-btn-sm {$auth->check('customer/import')?$auth->check('customer/import'):'hide'}"
                        title="导入" data-type="import"><i
                        class="iconfont icon-daoru4"></i>导入
                </button>
                <button class="layui-btn layuiadmin-btn-admin layui-btn-sm {$auth->check('customer/del')?$auth->check('customer/del'):'hide'}"
                        title="删除" data-type="batchdel"><i
                        class="layui-icon layui-icon-delete"></i>删除
                </button>
            </div>

            <table id="LAY-user-back-manage" lay-filter="LAY-user-back-manage"></table>
            <script type="text/html" id="table-useradmin-admin">
                <a class="layui-btn layui-btn-normal layui-btn-xs {$auth->check('customer/detail')?$auth->check('customer/detail'):'hide'}" lay-event="detail"><i class="layui-icon layui-icon-tips"></i>查看</a>
                <a class="layui-btn layui-btn-normal layui-btn-xs {$auth->check('customer/edit')?$auth->check('customer/edit'):'hide'}" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
                <a class="layui-btn layui-btn-danger layui-btn-xs {$auth->check('customer/del')?$auth->check('customer/del'):'hide'}" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>

            </script>
        </div>
    </div>
</div>
<script src="/static/layuiadmin/layui/layui.js"></script>
<script>
    var listUrl = "{:url('customer/index')}", create = "{:url('customer/add')}", edit = "{:url('customer/edit')}",
        deleteUrl = "{:url('customer/del')}", detailContent = "{:url('customer/detail')}",createFollowUp = "{:url('customer/followUp')}",deliver = "{:url('customer/deliver')}";
    layui.config({
        base: '/static/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'table', 'soulTable'], function () {
        var $ = layui.$
            , form = layui.form
            , table = layui.table, soulTable = layui.soulTable;
        table.render({
            elem: "#LAY-user-back-manage", url: listUrl
               ,overflow: {
                    type: 'tips'
                    // ,hoverTime: 500 // 悬停时间，单位ms, 悬停 hoverTime 后才会显示，默认为 0
                    ,color: 'white' // 字体颜色
                    ,bgColor: '#009688' // 背景色
                }
            , cellMinWidth: 100
            ,size:"sm"
            , cols: [
                [
                    {type: "checkbox", fixed: "left"},
                    {field: "id", align: "center", title: "id"},
                    {field: "customer_name", align: "center",width: 200,  title: "客户名称", event: 'setSign'},
                    {field: "city_id", align: "center", width: 200, title: "所在城市",templet:function(d){
                        if (d.CustomerCity != null) {
                            return d.CustomerCity.name;
                        }else{
                            return "Unknown";
                        }
                    }},
                    {field: "intention_id", align: "center",width: 200,  title: "意向产品",templet:function(d){
                        if (d.CustomerIntention != null) {
                            return d.CustomerIntention.name;
                        }else{
                            return "Unknown";
                        }
                    }},
                    {field: "customer_status_id", align: "center",width: 200,  title: "客户状态",templet:function(d){
                        if (d.CustomerStatus != null) {
                            return d.CustomerStatus.name;
                        }else{
                            return "Unknown";
                        }
                    }},
                    {field: "maturity_id", width: 200, title: "成熟度",templet:function(d){
                        if (d.CustomerRipe != null) {
                            return d.CustomerRipe.name;
                        }else{
                            return "Unknown";
                        }
                    }},
                    {field: "customer_phone",width: 200,  align: "center", title: "手机号"},
                    {field: "person_in_charge_id", width: 200, align: "center", title: "负责人",templet:function(d){
                        if (d.PersonInCharge != null) {
                            return d.PersonInCharge.nickname;
                        }else{
                            return "Unknown";
                        }
                    }},
                    {title: "操作", align: "center", fixed: "right", width: 300, toolbar: "#table-useradmin-admin"}]],
            response: {
                statusCode: 200 //重新规定成功的状态码为 200，table 组件默认为 0
            },
            parseData: function (res) { //将原始数据解析成 table 组件所规定的数据
                return {
                    "code": res.code,
                    "count": res.total, //解析数据长度
                    "data": res.data //解析数据列表
                };
            },done: function () {
                    soulTable.render(this)
                },
            text: {
                none: '呀！一条数据都没有'
            },
            page: true,
            limit: 15,
        });
        //按钮触发
        table.on("tool(LAY-user-back-manage)", function (e) {
            var param = $.param({'id': e.data.id});
            if ("edit" === e.event) {
                layer.open({
                    type: 2
                    , skin: 'layui-layer-molv'
                    , title: '编辑'
                    , content: edit + '?' + param
                    , area: ['100%', '100%']
                    , maxmin: true
                    , btn: ['确定', '取消']
                    , yes: function (index, layero) {
                        var iframeWindow = window['layui-layer-iframe' + index]
                            , submitID = 'LAY-user-back-submit'
                            , submit = layero.find('iframe').contents().find('#' + submitID);
                        //监听提交
                        iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                            var field = data.field; //获取提交的字段
                            //提交 Ajax 成功后，静态更新表格中的数据
                            $.ajax({
                                url: edit + '?' + param,
                                type: 'post',
                                data: field,
                                success: function (res) {
                                    if (res.code === 200) {
                                        layer.msg(res.msg, {icon: 1});
                                        table.reload('LAY-user-back-manage');
                                        layer.close(index); //关闭弹层
                                    } else {
                                        console.log(res);
                                        layer.msg(res.msg, {icon: 5});
                                        return false;
                                    }
                                }
                            });
                        });
                        submit.trigger('click');
                    }
                });
            } else if ("del" === e.event) {
                var params = $.param({'ids': e.data.id});
                layer.confirm("确定删除此客户吗？", function (t) {
                    $.ajax({
                        url: deleteUrl + '?' + params, success: function (res) {
                            if (res.code === 200) {
                                layer.msg(res.msg, {icon: 1});
                                table.reload('LAY-user-back-manage');
                                layer.close(t); //关闭弹层
                            } else {
                                console.log(res);
                                layer.msg(res.msg, {icon: 5});
                                return false;
                            }
                        }
                    });
                })
            } else if ("setSign" === e.event) {
                layer.open({
                    type: 2
                    , skin: 'layui-layer-molv'
                    , title: '预览'
                    , content: detailContent + '?' + param
                    , area: ['100%', '100%']
                    , maxmin: true
                });
            } else if ("detail" === e.event) {
                layer.open({
                    type: 2
                    , skin: 'layui-layer-molv'
                    , title: '预览'
                    , content: detailContent + '?' + param
                    , area: ['100%', '100%']
                    , maxmin: true
                });
            }
        });

        //监听搜索
        form.on('submit(LAY-user-back-search)', function (data) {
            var field = data.field;
            //执行重载
            table.reload('LAY-user-back-manage', {
                where: field
            });
        });

        //事件
        var active = {
            batchdel: function () {
                var checkStatus = table.checkStatus('LAY-user-back-manage')
                    , checkData = checkStatus.data; //得到选中的数据
                if (checkData.length === 0) {
                    return layer.msg('请选择数据');
                }
                var id = ''; //task_id
                for (var i = 0; i < checkData.length; i++) {
                    id += checkData[i].id + ",";
                }
                id = id.substring(0, id.length - 1);
                var ids = $.param({'ids': id});
                layer.confirm('确定删除吗？', function (index) {
                    //执行 Ajax 后重载
                    $.ajax({
                        url: deleteUrl,
                        type: "post",
                        data: ids,
                        success: function (res) {
                            if (res.code === 200) {
                                layer.msg(res.msg, {icon: 1});
                                table.reload('LAY-user-back-manage');
                                layer.close(index); //关闭弹层
                            } else {
                                console.log(res);
                                layer.msg(res.msg, {icon: 5});
                                return false;
                            }
                        }
                    });
                    // table.reload('LAY-user-back-manage');
                    // layer.msg('已删除');
                });
            }
            , add: function () {
                layer.open({
                    type: 2
                    , skin: 'layui-layer-molv'
                    , title: '添加新客户'
                    , content: create
                    , area: ['100%', '100%']
                    , maxmin: true
                    , btn: ['确定', '取消']
                    , yes: function (index, layero) {
                        var iframeWindow = window['layui-layer-iframe' + index]
                            , submitID = 'LAY-user-back-submit'
                            , submit = layero.find('iframe').contents().find('#' + submitID);
                        //监听提交
                        iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                            var field = data.field; //获取提交的字段
                            //提交 Ajax 成功后，静态更新表格中的数据
                            $.ajax({
                                url: create,
                                type: 'post',
                                data: field,
                                success: function (res) {
                                    if (res.code === 200) {
                                        layer.msg(res.msg, {icon: 1});
                                        table.reload('LAY-user-back-manage');
                                        layer.close(index); //关闭弹层
                                    } else {
                                        console.log(res);
                                        layer.msg(res.msg, {icon: 5});
                                        return false;
                                    }
                                }
                            });
                        });
                        submit.trigger('click');
                    }
                });
            }, refresh: function () {
                table.reload('LAY-user-back-manage');
            },
            follow_up:function(){
                var checkStatus = table.checkStatus('LAY-user-back-manage')
                    , checkData = checkStatus.data; //得到选中的数据
                if (checkData.length === 0) {
                    return layer.msg('请选择数据');
                }
                if (checkData.length>1) {
                    return layer.msg('写跟进任务只能选择一条数据');
                }
                var id = ''; //task_id
                for (var i = 0; i < checkData.length; i++) {
                    id += checkData[i].id + ",";
                }
                id = id.substring(0, id.length - 1);
                var param = $.param({'id': id});
                layer.open({
                    type: 2
                    , skin: 'layui-layer-molv'
                    , title: '跟进任务'
                    , content: createFollowUp + '?' +param
                    , area: ['100%', '100%']
                    , maxmin: true
                    , btn: ['确定', '取消']
                    , yes: function (index, layero) {
                        var iframeWindow = window['layui-layer-iframe' + index]
                            , submitID = 'LAY-user-back-submit'
                            , submit = layero.find('iframe').contents().find('#' + submitID);
                        //监听提交
                        iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                            var field = data.field; //获取提交的字段
                            //提交 Ajax 成功后，静态更新表格中的数据
                            $.ajax({
                                url: createFollowUp + '?' +param,
                                type: 'post',
                                data: field,
                                success: function (res) {
                                    if (res.code === 200) {
                                        layer.msg(res.msg, {icon: 1});
                                        table.reload('LAY-user-back-manage');
                                        layer.close(index); //关闭弹层
                                    } else {
                                        console.log(res);
                                        layer.msg(res.msg, {icon: 5});
                                        return false;
                                    }
                                }
                            });
                        });
                        submit.trigger('click');
                    }
                });
            },
            deliver:function(){
                var checkStatus = table.checkStatus('LAY-user-back-manage')
                    , checkData = checkStatus.data; //得到选中的数据
                if (checkData.length === 0) {
                    return layer.msg('请选择数据');
                }
                var id = '';
                for (var i = 0; i < checkData.length; i++) {
                    id += checkData[i].id + ",";
                }
                id = id.substring(0, id.length - 1);
                var param = $.param({'ids': id});                
                layer.open({
                    type: 2
                    , skin: 'layui-layer-molv'
                    , title: '项目转交'
                    , content: deliver + '?' +param
                    , area: ['100%', '100%']
                    , maxmin: true
                    , btn: ['确定', '取消']
                    , yes: function (index, layero) {
                        var iframeWindow = window['layui-layer-iframe' + index]
                            , submitID = 'LAY-user-back-submit'
                            , submit = layero.find('iframe').contents().find('#' + submitID);
                        //监听提交
                        iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                            var field = data.field; //获取提交的字段
                            //提交 Ajax 成功后，静态更新表格中的数据
                            $.ajax({
                                url: deliver + '?' +param,
                                type: 'post',
                                data: field,
                                success: function (res) {
                                    if (res.code === 200) {
                                        layer.msg(res.msg, {icon: 1});
                                        table.reload('LAY-user-back-manage');
                                        layer.close(index); //关闭弹层
                                    } else {
                                        console.log(res);
                                        layer.msg(res.msg, {icon: 5});
                                        return false;
                                    }
                                }
                            });
                        });
                        submit.trigger('click');
                    }
                });
            },
            export:function(){
                layer.msg("开通中");
            },
            import:function(){
                layer.msg("开通中");
            }
        };
        $('.layui-btn.layuiadmin-btn-admin').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });
</script>